package com.pay.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pay.common.base.BaseService;
import com.pay.common.support.Convert;
import com.pay.mapper.auto.PayOrderMapper;
import com.pay.model.auto.PayOrder;
import com.pay.model.auto.PayOrderExample;
import com.pay.model.custom.Tablepar;
import com.pay.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 支付订单Service
 *
 * @author fuce
 * @Title: PayOrderService.java 
 * @Package com.pay.service 
 * @email 87766867@qq.com
 * @date 2019-06-24 10:54:26  
 */
@Service
public class PayOrderService implements BaseService<PayOrder, PayOrderExample> {
    @Autowired
    private PayOrderMapper payOrderMapper;

    public void invalidByMoney(String money) throws Exception {
        payOrderMapper.updateByPrimaryKeySelective(PayOrder.builder()
                .valid(false)
                .money(BigDecimal.valueOf(Double.valueOf(money)))
                .build());
    }

    /**
     * 分页查询
     *
     * @param tablepar
     * @param money
     * @return
     */
    public PageInfo<PayOrder> list(Tablepar tablepar, BigDecimal money) {
        PayOrderExample testExample = new PayOrderExample();
        testExample.setOrderByClause("id ASC");
        PayOrderExample.Criteria criteria = testExample.createCriteria();
        if (money != null && !"".equals(money)) {
            criteria.andMoneyLike("%" + money + "%");
        }

//        Date yesToday=new Date(System.currentTimeMillis()-1000*60*60*24);
//        criteria.andCreateDateBetween(yesToday, new Date());
        criteria.andPayStatusEqualTo(0);

        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<PayOrder> list = payOrderMapper.selectByExample(testExample);
        PageInfo<PayOrder> pageInfo = new PageInfo<PayOrder>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {
        List<String> lista = Convert.toListStrArray(ids);
        PayOrderExample example = new PayOrderExample();
        example.createCriteria().andIdIn(lista);
        return payOrderMapper.deleteByExample(example);
    }


    @Override
    public PayOrder selectByPrimaryKey(String id) {

        return payOrderMapper.selectByPrimaryKey(id);
    }


    @Override
    public int updateByPrimaryKeySelective(PayOrder record) {
        return payOrderMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 添加
     */
    @Override
    public int insertSelective(PayOrder record) {
        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());
        return payOrderMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(PayOrder record, PayOrderExample example) {

        return payOrderMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(PayOrder record, PayOrderExample example) {

        return payOrderMapper.updateByExample(record, example);
    }

    @Override
    public List<PayOrder> selectByExample(PayOrderExample example) {

        return payOrderMapper.selectByExample(example);
    }


    @Override
    public long countByExample(PayOrderExample example) {

        return payOrderMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(PayOrderExample example) {

        return payOrderMapper.deleteByExample(example);
    }

    /**
     * 检查name
     *
     * @param payOrder
     * @return
     */
    public int checkNameUnique(PayOrder payOrder) {
        PayOrderExample example = new PayOrderExample();
        List<PayOrder> list = payOrderMapper.selectByExample(example);
        return list.size();
    }


}
